package cn.edu.scut.sys.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.scut.sys.dao.LeaderDao;
import cn.edu.scut.sys.service.LeaderService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.vo.Leader;

@Component("leaderService")
public class LeaderServiceImpl implements LeaderService{

private LeaderDao leaderDao;
	
	@Resource(name="leaderDao")
	public void setUserDao(LeaderDao leaderDao) {
		this.leaderDao = leaderDao;
	}

	@Override
	public DataResponse<Leader> list(DataRequest request) {
		DataResponse<Leader> response = new DataResponse<Leader>();  
        int count;//总记录数  
        int limit = request.getRows() <= 0 ? 20 : request.getRows();//每页显示数量  
        int totalPages;//总页数  
        int page = request.getPage() <= 0 ? 1 : request.getPage();//当前显示页码  
        List<Leader> list;  
          
        count = leaderDao.count().intValue();  
        totalPages = count / limit;  
        if (count % limit != 0) {  
            totalPages++;  
        }  
        int currPage = Math.min(totalPages, page);  
          
        int start = currPage * limit - limit;  
        start = start < 0 ? 0 : start;  
          
        list = leaderDao.list(start, limit, request.getSord(), request.getSidx());  
        response.setRecords(count);  
        response.setTotal(totalPages);  
        response.setPage(currPage);  
        response.setRows(list);  
          
        return response;  
	}

	@Override
	public boolean add(Leader leader) {
		leaderDao.insert(leader);
		return true;
	}

	@Override
	public Leader get(Long id) {
		return leaderDao.get(id);
	}

	@Override
	public boolean update(Leader leader) {
		leaderDao.update(leader);
		return true;
	}
	
	@Override
	public List<Leader> getAll() {
		return leaderDao.getAll();
	}
	
}
